﻿using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;

public static partial class Tools
{
    private static Dictionary<string, bool> s_ResourcesExistDict = new Dictionary<string, bool>();

    /// <summary>
    /// 检查资源是否存在，并缓存
    /// </summary>
    public static async UniTask<bool> CheckResourceExistAsync(string resourcePath)
    {
        if (!s_ResourcesExistDict.TryGetValue(resourcePath, out var exist))
        {
            var locations = await Addressables.LoadResourceLocationsAsync(resourcePath).Task;
            exist = locations.Count != 0;
            s_ResourcesExistDict[resourcePath] = exist;
        }
        return exist;
    }

    /// <summary>
    /// 同步检查资源是否存在，并缓存
    /// </summary>
    public static bool CheckResourceExist(string resourcePath)
    {
        if (!s_ResourcesExistDict.TryGetValue(resourcePath, out var exist))
        {
            var locations = Addressables.LoadResourceLocationsAsync(resourcePath).WaitForCompletion();
            exist = locations.Count != 0;
            s_ResourcesExistDict[resourcePath] = exist;
        }
        return exist;
    }
}